package com.exp.controller;

import com.exp.dto.UserInfDTO;
import com.exp.dto.UserPageQueryDTO;
import com.exp.pojo.UserInf;
import com.exp.result.PageResult;
import com.exp.result.Result;
import com.exp.result.StatusCode;
import com.exp.service.UserInfService;
import com.exp.utils.Message;
import com.exp.vo.UserInfVO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * 用户管理模块-why
 */
@RestController
@RequestMapping("/admin/userInf")
@Slf4j
public class UserInfController {

    @Autowired
    private UserInfService userInfService;

    /*
     * 用户查询-分页查询
     * 搜索条件：用户名、用户身份
     * 接受参数：【用户名、用户身份 页码，每页显示记录数】- UserPageQueryDTO
     * 返回参数：PageResult
     */
//    @CrossOrigin(origins = "http://172.20.10.6:5173")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody UserPageQueryDTO userPageQueryDTO){
        System.out.println(userPageQueryDTO);
        PageResult pageResult = userInfService.pageQuery(userPageQueryDTO);
        System.out.println(pageResult);
        return Result.success(StatusCode.SUCCESS,Message.PAGE_SUCCESS,pageResult);
    }

    /*
     * 添加用户
     * 输入：姓名、状态、登录名、密码
     */
//    @CrossOrigin(origins = "http://172.20.10.6:5173")
    @PostMapping("/add")
    public Result<UserInfVO> save(@RequestBody UserInfDTO userInfDTO){
        log.info("新增员用户：{}",userInfDTO);
        UserInfVO userInfVO = userInfService.save(userInfDTO);
        return Result.success(StatusCode.SUCCESS,Message.SAVE_SUCCESS,userInfVO);
    }

    /*
     * 修改用户
     * 输入：姓名、状态、登录名
     */
//    @CrossOrigin(origins = "http://172.20.10.6:5173")
    @PutMapping("/update")
    public Result update(@RequestBody UserInfDTO userInfDTO){
        log.info("编辑用户信息：{}", userInfDTO);
        userInfService.update(userInfDTO);
        UserInf userInf = userInfService.getById(userInfDTO.getId());
        UserInfVO userInfVO = new UserInfVO();
        BeanUtils.copyProperties(userInf, userInfVO);
        return Result.success(StatusCode.SUCCESS,Message.UPDATE_SUCCESS,userInfVO);
    }
    /*
     * 批量删除用户
     * 输入：id 集合
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Integer> ids) {
        userInfService.deleteBatch(ids);
        return Result.success(StatusCode.SUCCESS,Message.DELETE_SUCCESS,ids);
    }

}
